package kp.rpc.spring.boot.stater.bootStrap;

import kp.rpc.bootstrap.ProviderBootStrap;
import kp.rpc.model.ServiceRegisterInfo;
import kp.rpc.spring.boot.stater.annotation.KpRpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.List;


@Slf4j
public class RpcProviderBootStrap implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> aClass = bean.getClass();
        KpRpcService annotation = aClass.getAnnotation(KpRpcService.class);
        if (annotation != null) {
            Class<?> interfaceClass = annotation.interfaceClass();
            if (interfaceClass == void.class) {
                interfaceClass = aClass.getInterfaces()[0];
            }
            ServiceRegisterInfo<Object> registerInfo = new ServiceRegisterInfo<>();
            registerInfo.setServiceName(interfaceClass.getName());
            registerInfo.setImplClass(aClass);
            //init->加载bean到服务
            ProviderBootStrap.init(List.of(registerInfo), false);
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
